
#region Imported Namespaces
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
#endregion

namespace PHSRAG.Utility
{
	#region Class Settings
	/// <summary>
	/// The Settings class provides exception-safe static methods used to retrieve the value of a named key
	/// from the application configuration file. Overloaded methods handle various types of keys. All these
	/// methods accept a default value which will be returned if the specified key is not found.
	/// Note that the applicaion configuration file associated with whatever application loads this assembly
	/// is the one which will be used to retrieve values from.
	/// </summary>
	public class Settings
	{
		#region Class Variables
		private static NameValueCollection	appSettings = ConfigurationManager.AppSettings;
		#endregion

		#region Public Methods
		/// <summary>
		/// Get the string value for the specified key in an exception-safe fashion from the application configuration
		/// file. If the key has not value, then the specified default value is used.
		/// </summary>
		/// <param name="keyName">Name of the key</param>
		/// <param name="defaultValue">Default value (only used if no value is found in the configuration file)</param>
		/// <returns>String value for the key if found, defaultValue otherwise</returns>
		public static string GetKeyValue(string keyName, string defaultValue)
		{
			try
			{
				string s = appSettings[keyName];
				if (s != null)
					return s;
			}
			catch
			{
			}
			return defaultValue;
		}

		/// <summary>
		/// Get the integer value for the specified key in an exception-safe fashion from the application configuration
		/// file. If the key has not value, then the specified default value is used.
		/// </summary>
		/// <param name="keyName">Name of the key</param>
		/// <param name="defaultValue">Default value (only used if no value is found in the configuration file)</param>
		/// <returns>Integer value for the key if found, defaultValue otherwise</returns>
		public static int GetKeyValue(string keyName, int defaultValue)
		{
			try
			{
				string s = appSettings[keyName];
				if (s != null)
					return Convert.ToInt32(s);
			}
			catch
			{
			}
			return defaultValue;
		}

		/// <summary>
		/// Get the boolean value for the specified key in an exception-safe fashion from the application configuration
		/// file. If the key has not value, then the specified default value is used.
		/// </summary>
		/// <param name="keyName">Name of the key</param>
		/// <param name="defaultValue">Default value (only used if no value is found in the configuration file)</param>
		/// <returns>Boolean value for the key if found, defaultValue otherwise</returns>
		public static bool GetKeyValue(string keyName, bool defaultValue)
		{
			try
			{
				string s = appSettings[keyName];
				if (s != null)
					return Convert.ToBoolean(s);
			}
			catch
			{
			}
			return defaultValue;
		}
		#endregion
	}
	#endregion
}

// --- EOF ---
